
function y = difBFS(w,N,P,X,Z,R,C)

PZZ = calculatePHat(w,X,Z,R,C);

y = 0;

for ii=1:R
    for jj=1:C
        y = y + myfunc(ii,jj,N,P,PZZ);
    end
end

end



function v = myfunc(ii,jj,N,P,PZ)

p = P(ii,jj);
pHat = PZ(ii,jj);
n = N(ii,jj);
v = n * (p - pHat)^2 / pHat*(1-pHat) ;

end

